package com.feeyo.groundservice.composewidget.view.drag_list.list

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.unit.dp

/**
 *作者：70893 on 2025-06-10
 *描述：一般列表拖动
 */

/**
 * 一般列表拖动
 *
 * @param items 列表数据
 * @param draggable 是否可拖动
 * @param onDragStart 拖动开始回调
 * @param onDrag 拖动中回调
 * @param onDragEnd 拖动结束回调
 * @param horizontalAlignment 列表水平对齐方式
 * @param verticalArrangement 列表垂直分布方式
 * @param contentPadding 内边距
 * @param itemContent 列表项内容
 * **/
@Composable
fun <T> DragDropLazyColumn(
    modifier: Modifier = Modifier,
    items: List<T>,
    draggable: Boolean = true,
    onDragStart: () -> Unit,
    onDrag: (Int, Int) -> Unit,
    onDragEnd: () -> Unit,
    horizontalAlignment: Alignment.Horizontal = Alignment.CenterHorizontally,
    verticalArrangement: Arrangement.HorizontalOrVertical = Arrangement.spacedBy(16.dp),
    contentPadding: PaddingValues = PaddingValues(horizontal = 24.dp, vertical = 16.dp),
    itemContent: (@Composable BoxScope.(modifier: Modifier, item: T, isDragging: Boolean) -> Any)
) {
    val listState = rememberLazyListState()
    val dragDropState = rememberDragDropState(listState, onDragStart,onDrag, onDragEnd)
    LazyColumn(
        modifier = Modifier.then(modifier).let {
            if (draggable) {
                it.dragContainer(dragDropState)
            } else
                it
        },
        state = listState,
        contentPadding = contentPadding,
        horizontalAlignment = horizontalAlignment,
        verticalArrangement = verticalArrangement,
    ) {
        itemsIndexed(items, key = { _, item -> item.toString() }) { index, item ->
            DraggableItem(
                dragDropState = dragDropState,
                index = index
            ) {
                val xScale = animateFloatAsState(
                    targetValue = if (it) 1.05f else 1f,
                    label = "size",
                )

                val yScale = animateFloatAsState(
                    targetValue = if (it) 1.2f else 1f,
                    label = "size",
                )

                val itemModifier = Modifier.scale(xScale.value, yScale.value)

                itemContent(itemModifier, item, it)
            }
        }
    }
}